package com.android.activity.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.activity.DeliverActivity;
import com.android.R;
import com.android.activity.LoginActivity;
import com.android.bean.ResponseData;
import com.android.bean.User;
import com.android.databinding.FragmentMyPageBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyPageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyPageFragment extends Fragment implements View.OnClickListener{
    private FragmentMyPageBinding binding;
    private View rootView;
    private TextView textView;
    private User user;


    private View view;



    public MyPageFragment() {
        // Required empty public constructor
    }

    @Override
    public void onStart() {
        super.onStart();


    }

    @Override
    public void onStop() {
        super.onStop();


    }


    public static MyPageFragment newInstance(String param1, String param2) {
        MyPageFragment fragment = new MyPageFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onDestroy() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        super.onDestroy();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }


    }

    @Override
    public void onDestroyView() {
        binding = null;
        super.onDestroyView();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding = FragmentMyPageBinding.inflate(inflater,container,false);
        view = binding.getRoot();

         TextView tvName = view.findViewById(R.id.tv_name);
        Button rece_addr = view.findViewById(R.id.btn_my_recevive_goods);
        rece_addr.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),
                        DeliverActivity.class);
                startActivity(intent);
            }
        });
        Button log_out = view.findViewById(R.id.btn_logout);
        log_out.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),
                        LoginActivity.class);
                startActivity(intent);
            }
        });
        tvName.setText(user.getUserName());
        binding.btnMyReleasedGoods.setOnClickListener(this);
        binding.btnMyBuyGoods.setOnClickListener(this);
        return view;
    }

    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
    public void getLoginUser(ResponseData<User> responseData){
        this.user = responseData.getData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_my_buy_goods:
                Log.e("1","btn_my_buy_goods");
                break;
            case R.id.btn_my_released_goods:
                Log.e("1","btn_my_recevive_goods");
                break;
        }


    }
}